<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <div class="title">黑马面面</div>
        <div class="line"></div>
        <div class="sub-title">用户登录</div>
      </div>
      <div class="login">
        <el-form
          :model="form"
          ref="form"
          :rules="rules"
          :inline="false"
          size="normal"
        >
          <el-form-item prop="phone">
            <el-input
              prefix-icon="el-icon-user"
              v-model="form.phone"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              show-password
              v-model="form.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="16">
              <el-form-item prop="code">
                <el-input
                  prefix-icon="el-icon-user"
                  v-model="form.code"
                  placeholder="请输入验证码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <img :src="loginSrc" alt="" class="captcha" @click="btnSrc" />
            </el-col>
          </el-row>
          <el-form-item prop="checked">
            <el-checkbox v-model="form.checked"></el-checkbox>
            <span>我已阅读并同意</span>
            <el-link
              type="primary"
              href="https://element.eleme.io"
              target="_blank"
              >用户协议</el-link
            >
            <span>和</span>
            <el-link
              type="primary"
              href="https://element.eleme.io"
              target="_blank"
              >隐私条款</el-link
            >
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:100%;" @click="login"
              >登录</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width:100%;"
              @click="$refs.registerRef.dialogVisible = true"
              >注册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <register ref="registerRef"></register>
  </div>
</template>
<script>
import { saveToken } from '../../utils/token.js'
import register from './register'
export default {
  components: {
    register
  },
  name: 'login',
  data () {
    return {
      loginSrc: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      form: {
        phone: '',
        password: '',
        code: '',
        checked: false
      },
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              // rule  校验规则
              // value  输入的值
              // callback 回调函数  带参数的callback返回错误信息    不带参数callback是正确
              if (!value) {
                callback(new Error('手机号码不能为空'))
                return
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                callback(new Error('请输入正确的手机格式'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度在6-16位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证不能为空', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    btnSrc () {
      this.loginSrc =
        process.env.VUE_APP_BASEURL + '/captcha?type=login&r=' + Date.now()
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return

        const res = await this.$axios.post('/login', this.form)
        //发请求
        console.log(res)
        if (res.code === 200) {
          //成功
          this.$message.success('登陆成功')
          // 保存token
          // localStorage.setItem('heimamm66-token', res.data.token)
          saveToken(res.data.token)
          console.log(this.$router)
          // 跳转到layout
          this.$router.push('/layout')
        } else {
          //提示错误信息
          this.$message.error(res.message)
          this.$message({
            message: res.message,
            type: 'error',
            duration: 1000
          })
          this.btnSrc()
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login {
      margin-top: 30px;
    }
    .captcha {
      margin-left: 10px;
      width: 100%;
      height: 40px;
    }
    .loginBtn {
      width: 100%;
    }
  }
}
</style>
